﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using nelech.Models.DBModel;
using System.Globalization;
using nelech.Controllers.Utils;
using System.Reflection;
using System.IO;

namespace nelech.Views.Shared.Code
{
    public class Utils
    {
        protected static nelechDBEntities db = new nelechDBEntities();

        public static SelectListItem[] CreateTypeListItems(int? selected)
        {
            var objs = from p in db.Type select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateNeviaListItems(int? selected)
        {
            var objs = from p in db.Nevia select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateDurationItem(int? selected)
        {
            var objs = from p in db.Duration select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateCircleItem(int? selected)
        {
            var objs = from p in db.Circle select new { Id = p.id, Text = p.circle1 };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }
        public static SelectListItem[] CreateDifficultyItem(int? selected)
        {
            var objs = from p in db.Difficulty select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }
        public static SelectListItem[] CreateTrafficItem(int? selected)
        {
            var objs = from p in db.Traffic select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateSubjectItem(int? selected)
        {
            var objs = from p in db.Subject select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateTimeItem(int? selected)
        {
            var objs = from p in db.Time select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }
        public static SelectListItem[] CreateSeasonListItems(int? selected)
        {
            var objs = from p in db.Season select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }
        public static SelectListItem[] CreateContactListItems()
        {

            var items = new SelectListItem[] {
                new SelectListItem(){Text=GetContactSubjectById(0) , Value="0", Selected= true},
                new SelectListItem(){Text=GetContactSubjectById(1) , Value="1"},
                new SelectListItem(){Text=GetContactSubjectById(2) , Value="2"}
            };

            return items;
        }
        public static String GetContactSubjectById(int id)
        {
            switch (id)
            {
                case 0:
                default:
                    return "הארה / הערה";
                case 1:
                    return "הצעת מסלול / מקום חדש";
                case 2:
                    return "מעוניינים במידע חדש";
            }
        }

        public static SelectListItem[] CreateCostListItems(int? selected)
        {
            var objs = from p in db.Cost select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreatePuzzleListItems(int? selected)
        {
            var objs = from p in db.PuzzleTopic select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateSizeListItems(int? selected)
        {
            var objs = from p in db.Size select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateRegeonListItems(int? selected)
        {
            var objs = from p in db.Regeon select new { Id = p.id, Text = p.text };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static SelectListItem[] CreateAttrTypeListItems(int? selected)
        {
            var objs = from p in db.AttractionType select new { Id = p.id, Text = p.name };
            var items = (from o in objs.AsEnumerable() select new SelectListItem() { Value = o.Id.ToString(), Text = o.Text }).ToArray();

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static IEnumerable<SelectListItem> CreateYearListItems(int? selected)
        {
            return CreateNumericRangeListItems(2010, 2020, 1, selected);
        }

        public static IEnumerable<SelectListItem> CreateMonthListItems(int? selected)
        {
            var items = new List<SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "ינואר",
                    Value = "1",
                    Selected = true
                },
                new SelectListItem()
                {
                    Text = "פברואר",
                    Value = "2",
                },
                new SelectListItem()
                {
                    Text = "מרץ",
                    Value = "3",
                },
                new SelectListItem()
                {
                    Text = "אפריל",
                    Value = "4",
                },
                new SelectListItem()
                {
                    Text = "מאי",
                    Value = "5",
                },
                new SelectListItem()
                {
                    Text = "יוני",
                    Value = "6",
                },
                new SelectListItem()
                {
                    Text = "יולי",
                    Value = "7",
                },
                new SelectListItem()
                {
                    Text = "אוגוסט",
                    Value = "8",
                },
                new SelectListItem()
                {
                    Text = "ספטמבר",
                    Value = "9",
                },
                new SelectListItem()
                {
                    Text = "אוקטובר",
                    Value = "10",
                },
                new SelectListItem()
                {
                    Text = "נובמבר",
                    Value = "11",
                },
                new SelectListItem()
                {
                    Text = "דצמבר",
                    Value = "12",
                }                
            };

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static IEnumerable<SelectListItem> CreateDayListItems(int? selected)
        {
            return CreateNumericRangeListItems(1, 31, 1, selected);
        }

        private static IEnumerable<SelectListItem> CreateNumericRangeListItems(int start, int end, int interval, int? selected)
        {
            var items = new List<SelectListItem>();
            for (var i = start; i <= end; i += interval)
            {
                var item = new SelectListItem()
                {
                    Text = i.ToString(),
                    Value = i.ToString()
                };
                items.Add(item);
            }

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static IEnumerable<SelectListItem> CreateHourListItems(int? selected)
        {
            return CreateNumericRangeListItems(0, 23, 1, selected);
        }

        public static IEnumerable<SelectListItem> CreateMinuteListItems(int? selected)
        {
            return CreateNumericRangeListItems(0, 59, 5, selected);
        }
        public static IEnumerable<SelectListItem> CreateWaterListItems(int? selected)
        {
            var items = new SelectListItem[] {
                new SelectListItem() { Text = "לא משנה", Value = "0", Selected = true },
                new SelectListItem() { Text = "עם מים", Value = "1"}, 
                new SelectListItem() { Text = "ללא מים", Value = "2"} 
            };

            if (selected != null && selected.HasValue)
            {
                items.First(p => p.Value == selected.Value.ToString()).Selected = true;
            }

            return items;
        }

        public static string GetHebrewJewishDateString(DateTime anyDate, bool addDayOfWeek)
        {
            System.Text.StringBuilder hebrewFormatedString = new System.Text.StringBuilder();

            // Create the hebrew culture to use hebrew (Jewish) calendar 
            CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");
            jewishCulture.DateTimeFormat.Calendar = new HebrewCalendar();

            #region Format the date into a Jewish format

            if (addDayOfWeek)
            {
                // Day of the week in the format " " 
                hebrewFormatedString.Append(anyDate.ToString("dddd", jewishCulture) + " ");
            }

            // Day of the month in the format "'" 
            hebrewFormatedString.Append(anyDate.ToString("dd", jewishCulture) + " ");

            // Month and year in the format " " 
            hebrewFormatedString.Append("" + anyDate.ToString("y", jewishCulture));

            #endregion

            return hebrewFormatedString.ToString();
        }

        public static string GetHebrewJewishMonthString(DateTime anyDate, bool addDayOfWeek)
        {
            System.Text.StringBuilder hebrewFormatedString = new System.Text.StringBuilder();

            // Create the hebrew culture to use hebrew (Jewish) calendar 
            CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");
            jewishCulture.DateTimeFormat.Calendar = new HebrewCalendar();

            #region Format the date into a Jewish format



            // Day of the month in the format "'" 
            hebrewFormatedString.Append("" + anyDate.ToString("y", jewishCulture));

            // Month and year in the format " " 

            #endregion

            return hebrewFormatedString.ToString();
        }

        public static AdvertsPlaces AdvertsPacageHP()
        {
            var advertsTop = new List<Advert>();
            var advertsRight = new List<Advert>();
            var advertsLeft = new List<Advert>();
            var advertsButtom = new List<Advert>();
            foreach (var advert in db.Advert.Where(p => p.publish == 1))
            {
                if (advert.date > DateTime.Now.Date)
                {
                    advert.AdvertPlace.Load();
                    if (advert.AdvertPlace.Any(p => p.id == 1))
                        advertsTop.Add(advert);
                    if (advert.AdvertPlace.Any(p => p.id == 2))
                        advertsRight.Add(advert);
                    if (advert.AdvertPlace.Any(p => p.id == 3))
                        advertsLeft.Add(advert);
                    if (advert.AdvertPlace.Any(p => p.id == 4))
                        advertsButtom.Add(advert);
                }
            }
            return AdvertPac(advertsTop, advertsRight, advertsLeft, advertsButtom);
        }
        public static AdvertsPlaces AdvertsPacage()
        {
            var advertsTop = new List<Advert>();
            var advertsRight = new List<Advert>();
            var advertsLeft = new List<Advert>();
            var advertsButtom = new List<Advert>();
            foreach (var advert in db.Advert.Where(p => p.publish == 1))
            {
                if (advert.date > DateTime.Now.Date)
                {
                    advert.AdvertPlace.Load();
                    if (advert.AdvertPlace.Any(p => p.id == 5))
                        advertsTop.Add(advert);
                    if (advert.AdvertPlace.Any(p => p.id == 6))
                        advertsRight.Add(advert);
                    if (advert.AdvertPlace.Any(p => p.id == 7))
                        advertsLeft.Add(advert);
                    if (advert.AdvertPlace.Any(p => p.id == 8))
                        advertsButtom.Add(advert);
                }
            }
            return AdvertPac(advertsTop, advertsRight, advertsLeft, advertsButtom);
        }
        public static AdvertsPlaces AdvertPac(List<Advert> advertsTop, List<Advert> advertsRight, List<Advert> advertsLeft, List<Advert> advertsButtom)
        {
            Random random = new Random();
            Advert advertButtom = null;
            Advert advertTop = null;
            Advert advertRight = null;
            Advert advertLeft = null;
            if (advertsTop.Count() > 0)
            {
                int num = random.Next(0, advertsTop.Count());
                advertTop = advertsTop[num];
            }
            if (advertsRight.Count() > 0)
            {
                int num = random.Next(0, advertsRight.Count());
                advertRight = advertsRight[num];
            }
            if (advertsLeft.Count() > 0)
            {
                int num = random.Next(0, advertsLeft.Count());
                advertLeft = advertsLeft[num];
            }
            if (advertsButtom.Count() > 0)
            {
                int num = random.Next(0, advertsButtom.Count());
                advertButtom = advertsButtom[num];
            }

            var advertsPlaces = new AdvertsPlaces() { advertLeft = advertLeft, advertButtom = advertButtom, advertRight = advertRight, advertTop = advertTop };
            return advertsPlaces;
        }

        public static String JMonth(int month)
        {
            switch (month)
            {
                case 1:
                    return "תשרי";
                case 2:
                    return "חשוון";
                case 3:
                    return "כסלו";
                case 4:
                    return "טבת";
                case 5:
                    return "שבט";
                case 6:
                    return "אדר";
                case 7:
                    return "ניסן";
                case 8:
                    return "אייר";
                case 9:
                    return "סיוון";
                case 10:
                    return "תמוז";
                case 11:
                    return "אב";
                case 12:
                default:
                    return "אלול";

            }
        }
        public static String RegeonWithOutThe(int regeon)
        {
            switch (regeon)
            {
                case 1:
                    return "בחרמון, בגולן ובאצבע הגליל";
                case 2:
                    return "בגליל העליון";
                case 3:
                    return "בגליל התחתון ובעמקים";
                case 4:
                    return "בכרמל";

                case 5:
                    return "בבקעת הירדן ובמזרח השומרון";
                case 7:
                    return "בשרון ובמערב השומרון";
                case 8:
                    return "בצפון מדבר יהודה ובים המלח";
                case 9:
                    return "במבואות ירושלים";
                case 10:
                    return "בגוש דן ובחוף פלשת";
                case 11:
                    return "בדרום מדבר יהודה ובים המלח";
                case 12:
                default:
                    return "בדרום שפלה ושולי הר חברון";
                case 13:
                    return "בנגב המערבי";
                case 14:
                    return "בצפון הערבה ומזרח הנגב";
                case 15:
                    return "בנגב התיכון";
                case 16:
                    return "בחולות וברמת הנגב";
                case 17:
                    return "בערבה התיכונה ומזרח הר הנגב";
                case 18:
                    return "במערב הר הנגב";
                case 19:
                    return "במישורי פארן";
                case 20:
                    return "באילת";

            }
        }
        public static string AdvertHeight(int id)
        {
            switch (id)
            {
                case 1:
                    return "70px";
                case 2:
                    return "";
                case 3:
                    return "70px";
                case 4:
                    return "70px";
                case 5:
                    return "";
                case 6:
                    return "";
                case 7:
                    return "70px";
                case 8:
                default:
                    return "";

            }
        }
        public static string AdvertWidth(int id)
        {
            switch (id)
            {
                case 1:
                    return "950px";
                case 2:
                    return "200px";
                case 3:
                    return "178px";
                case 4:
                    return "950px";
                case 5:
                    return "200px";
                case 6:
                    return "160px";
                case 7:
                    return "160px";
               
                default:
                    return "160px";


            }
        }

        public static string GetEmbeddedString(string name)
        {
            var assembly = Assembly.GetExecutingAssembly();
            using (var reader = new StreamReader(assembly.GetManifestResourceStream(name)))
            {
                return reader.ReadToEnd();
            }
        }


        public static string GetSubjectHtml(Forum item, int pid, List<Forum> modelList)
        {

            var image = string.IsNullOrEmpty(item.image) ? "" : "<img src=\"" + item.image + "\" />";
            var movie = string.IsNullOrEmpty(item.movie) ? "" : item.movie;
            var LittleImage = string.IsNullOrEmpty(item.image) ? "" : "<img src=\"" + item.image + "\" width='100px'/>";
            var LittleMovie = string.IsNullOrEmpty(item.movie) ? "" : "לסירטון";

            var children = modelList.Where(p => p.p_id == item.id);
            if (children == null)
                return "";
            var childHtml = "";
            foreach (var child in children)
            {
                childHtml += GetSubjectHtml(child, item.id, modelList);
            }

            return string.Format(
                "<li>" +
                "<div class='forumTop'><span class='forumTitle'><a id='response{11}' href='javascript:ShowResponse({11})'>{0}</a></span>" +
                "<span class=\"forumName\">{1}</span><span class='floatleft'>{2}</span></div>" +
                "<div id='text{11}' class='forunText' style='display:none'>" +
                "<span class='floatleft'><a href='javascript:showImg({11})'>{4}</a><div id='img{11}' style='display:none'><img src='{6}'></div><br/>" +
                "<a href='javascript:showMovie({11})'>{5}</a></span><div id='movie{11}' style='display:none'>{7}</div>{3}" +
                "<span class='tguva'><a href='\\Forum\\CreateResponse1?p_id={11}'>הוסף תגובה</a></span></div>" +
                "<ul>{10}</ul></li>",
                item.title,
                  item.name,
                GetHebrewJewishDateString(item.date, true),
                item.text,
                LittleImage,
                LittleMovie,
                image,
                movie,
                item.s_id,
                item.p_id,
                childHtml,
                item.id);
        }

        //public static string GetSubjectTitleHtml(Forum item, int pid, List<Forum> modelList)
        //{

        //    var children = modelList.Where(p => p.p_id == item.id);
        //    if (children == null)
        //        return "";
        //    var childHtml = "";
        //    foreach (var child in children)
        //    {
        //        childHtml += GetSubjectTitleHtml(child, item.id, modelList);
        //    }

        //    return string.Format(
        //        "<li class='forumTree'><div class='responseTitle'><b><a href='#{0}'>{1}</a></b><br/><span class=\"forumName1\">{2} {3}</span><br/>" +
        //        "<span id='text{0}' style='display:none'>{4}</span><ul>{5}</ul></li>",
        //       item.id,
        //        item.title,
        //        item.name,
        //        GetHebrewJewishDateString(item.date, true),
        //        item.text,
        //        childHtml);
        //}
    }
}
